package com.wintop.youyuan.util;

import java.util.Calendar;
import java.util.Date;

public class CalculateUtil {

	public static int getAge(String birthday) {// 获取年龄
		String year = birthday.substring(0, 4);
		int age = Calendar.getInstance().get(Calendar.YEAR)
				- Integer.parseInt(year);
		return age;
	}

	public static int getLoveAge(String loveAge) {// 获取搜缘年龄
		if (loveAge.equals("不要求")) {
			return 0;
		}
		String year = loveAge.substring(0, 2);
		int age = Integer.parseInt(year);
		return age;
	}

	public static String getBirthday(int age) {// 获取年龄
		int ages = Calendar.getInstance().get(Calendar.YEAR) - age;
		return "" + ages + "-1-1";
	}

	public static String getDeltaT(Date date) {// 获取相差时间
		long between = (Calendar.getInstance().getTime().getTime() - date
				.getTime()) / 1000;// 除以1000是为了转换成秒
		long day = between / (24 * 3600);
		long hour = between % (24 * 3600) / 3600;
		long minute = between % 3600 / 60;
		long second = between % 60 / 60;
		if (day > 0) {
			return day + "天";
		} else if (hour > 0) {
			return hour + "小时";
		} else if (minute > 0) {
			return minute + "分";
		} else if (second > 0) {
			return second + "秒";
		}
		/*
		 * System.out.println("" + day + "天" + hour + "小时" + minute + "分" +
		 * second + "秒");
		 */
		return null;
	}

	public static String getConstellation(String birthday) {// 获取星座
		String[] b = birthday.split("-");
		int month = Integer.parseInt(b[1]);
		int day = Integer.parseInt(b[2]);
		return getAstro(month, day);
	}

	/*
	 * 根据日期获取星座
	 */
	private static String getAstro(int month, int day) {
		String[] starArr = { "魔羯座", "水瓶座", "双鱼座", "牡羊座", "金牛座", "双子座", "巨蟹座",
				"狮子座", "处女座", "天秤座", "天蝎座", "射手座" };
		int[] DayArr = { 22, 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22 }; // 两个星座分割日
		int index = month;
		// 所查询日期在分割日之前，索引-1，否则不变
		if (day < DayArr[month - 1]) {
			index = index - 1;
		}
		// 返回索引指向的星座string
		return starArr[index];
	}

}
